from apps.system.payway.baseViews import *
from apps.interface.client.dao import ClientDao
from project.settings import build

class PaywayViewSet(BasePaywayViewSet):

    def get_queryset(self):
        _list = PaywayDao().findAll().select_related('paywayCode')
        return _list

    '''获取详情'''
    @action(methods=['post'], detail=False)
    def getPaywayInfo(self, request):
        params = getRequestParams(request)
        _payway = None
        if params.get('types'):
            _payways = PaywayDao().findAll().filter(user=request.user, types=params.get('types'))
            if _payways.exists():
                _payway = _payways.first()
            else:
                return Response(response_error('记录不存在'))
        return Response(response_success(PaywayModelSerializer(_payway).data))

    '''获取默认详情'''
    @action(methods=['post'], detail=False)
    def findDefaultPayway(self, request):
        params = getRequestParams(request)
        _defaultPayWays = PaywayDao().findAll().filter(user=request.user, default=True, types='bank')
        if _defaultPayWays.exists():
            return Response(response_success(PaywayModelSerializer(_defaultPayWays.first()).data))
        else:
            return Response(response_error('记录不存在'))

    '''新增'''
    @action(methods=['post'], detail=False)
    def createPayway(self, request):
        params = getRequestParams(request)
        # 一个用户下每种支付方式只能有一个账号
        params['user'] = request.user.id
        _payways = PaywayDao().findAll().filter(user=request.user, types=params.get('types'))
        if _payways.exists():
            _payways.update(is_delete=1)
        # 如果存在默认地址，把默认地址全置false
        if params.get('default') is True:
            _defaultPayWays = PaywayDao().findAll().filter(user=request.user, default=True)
            if _defaultPayWays.exists():
                _defaultPayWays.update(default=False)

        PaywayDao().create(params)
        return Response(response_success('新增成功'))

    '''更新信息'''
    @action(methods=['post'], detail=False)
    def updatePayway(self, request):
        params = getRequestParams(request)
        # 如果存在默认，把默认全置false
        if params.get('default') is True:
            _defaultPayWays = PaywayDao().findAll().filter(user=request.user, default=True)
            if _defaultPayWays.exists():
                _defaultPayWays.update(default=False)
        PaywayDao().update(params.get('paywayId'), params)
        return Response(response_success('更新成功'))
    

    '''获取配置信息'''
    @action(methods=['post'], detail=False,authentication_classes=[],permission_classes=[])
    def getConfig(self, request):
        params = getRequestParams(request)
        
        config = {
            'wx': {
                'title': '微信',
                'key': 'wx'
            }
        }
        config['ali'] = {
            'title': '支付宝',
            'key': 'ali'
        }
        config['bank'] = {
            'title': '银行卡',
            'key': 'bank'
        }
        client = ClientDao().findAll().filter(clientname=params.get('clientname')).first()
        if client.checking and params.get('build') == str(build):
            config = []
        return Response(response_success(config))